Skip to content

External middleware in cloudflare #3254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

conico974
Copy link
Contributor

@conico974 conico974 commented May 22, 2025

The goal of this PR is to deploy the routing/middleware layer in its own worker.
This should make cold start for ISR/SSG route that has been generated before really fast compared to now (On par or even better than in Vercel)
It requires #3265 to be merged before release.

Will close RND-7163

What's changed

  • The routing/middleware are in a separate worker. (ISR will be served from here)
  • The server and middleware worker are "linked" together. In preview, we use the preview url, in staging and prod, we use the Cloudflare-Workers-Version-Overrides headers (this requires the 2 versions to be uploaded first)
  • Deployment are now a multi step process to ensure that the middleware will always access the correct server
  • It doesn't use the binding anymore for the API, in some case it can cause a Response closed due to connection limit because the limit are shared between the workers and all its binding, especially for big pages. (This should close RND-7189)
  • This will also reduce memory consumption by the server worker since the middleware is in a different worker (it will help with RND-7141)

One of the downsides of this method is that changes to routes in wrangler.jsonc won't work automatically. It requires running wrangler triggers deploy

Also, we can't really test the Durable Object without deploying (DO don't work with preview url )

Copy link

changeset-bot bot commented May 22, 2025

⚠️ No Changeset found

Latest commit: 0585bf0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

argos-ci bot commented May 22, 2025

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
customers-v1 (Inspect) 👍 Changes approved 2 changed May 28, 2025, 11:06 AM
customers-v2 (Inspect) 👍 Changes approved 81 changed May 28, 2025, 11:07 AM
default (Inspect) ✅ No changes detected - May 28, 2025, 11:06 AM
v2-cloudflare (Inspect) ✅ No changes detected - May 28, 2025, 11:08 AM
v2-vercel (Inspect) ✅ No changes detected - May 28, 2025, 11:08 AM

Copy link

linear bot commented May 27, 2025

Fix NextRequest body being incorrect
Copy link

linear bot commented May 28, 2025

@conico974 conico974 changed the title [WIP] External middleware in cloudflare External middleware in cloudflare May 28, 2025
Copy link
Member

@jpreynat jpreynat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great 👌

Hopefully at some point the whole orchestration could be better handled at a lower level by opennext itself, but this should definitely be a great improvement in perfs for us

workingDirectory: ./
wranglerVersion: '4.10.0'
environment: ${{ inputs.environment }}
command: versions deploy ${{ steps.extract_current_version.outputs.version_id }}@100% ${{ inputs.serverVersionId }}@0% -y --config ./packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, too bad there isn't a "--no-traffic-update" flag 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this would have been very handy. This should get better in the future 🤞

@conico974
Copy link
Contributor Author

Hopefully at some point the whole orchestration could be better handled at a lower level by opennext itself, but this should definitely be a great improvement in perfs for us

Yeah this will get better, but to make it work in a more generic way, it requires a bunch of change, and not only to OpenNext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants